package com.hfy.kandian.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfy.kandian.entity.Book;
import com.hfy.kandian.entity.Bookshelf;
import com.hfy.kandian.entity.Writer;
import com.hfy.kandian.mapper.BookMapper;
import com.hfy.kandian.mapper.BookshelfMapper;
import com.hfy.kandian.mapper.WriterMapper;
import com.hfy.kandian.service.BookService;
import com.hfy.kandian.service.BookshelfService;
import com.hfy.kandian.service.SearchService;
import com.hfy.kandian.service.WriterService;
import io.swagger.models.auth.In;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    BookMapper bookMapper;

    @Autowired
    WriterMapper writerMapper;

    @Autowired
    BookshelfMapper bookshelfMapper;

    @Autowired
    BookService bookService;

    @Autowired
    WriterService writerService;


    @Override
    public Map<String, List> searchBookshlef(int userId, String keyword) {
        Bookshelf bookshelf = bookshelfMapper.selectOne(new LambdaQueryWrapper<Bookshelf>().eq(Bookshelf::getUserId,userId));
        Map<String,List> resultMap = new HashMap<>();
        List<Book> books = bookMapper.selectBatchIds(bookshelf.getBookIdList());
        resultMap.put("book",books);
        List<Writer> writers = writerMapper.selectBatchIds(books.stream().map(Book::getWriterId).collect(Collectors.toList()));
        resultMap.put("writer",writers);
        return resultMap;
    }

    @Override
    public Map<String, List> searchAll(String keyword) {
        Map<String, List> resultMap = new HashMap<>();
        resultMap.put("book",bookService.getBookByKeyword(keyword));
        resultMap.put("writer",writerService.getWriterByKeyword(keyword));
        return resultMap;
    }
}
